package net.youpo.Market.UI.Search;

import java.util.Random;

import net.youpo.Market.BaseActivity;
import net.youpo.Market.R;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SearchActivity extends BaseActivity implements OnClickListener {
	public static final String[] keywords = { "QQ", "Sodino", "APK", "GFW",
			"铅笔",//
			"短信", "桌面精灵", "MacBook Pro", "平板电脑", "雅诗兰黛",//
			"卡西欧 TR-100", "笔记本", "SPY Mouse", "Thinkpad E40", "捕鱼达人",//
			"内存清理", "地图", "导航", "闹钟", "主题",//
			"通讯录", "播放器", "CSDN leak", "安全", "3D",//
			"美女", "天气", "4743G", "戴尔", "联想",//
			"欧朋", "浏览器", "愤怒的小鸟", "mmShow", "网易公开课",//
			"iciba", "油水关系", "网游App", "互联网", "365日历",//
			"脸部识别", "Chrome", "Safari", "中国版Siri", "A5处理器",//
			"iPhone4S", "摩托 ME525", "魅族 M9", "尼康 S2500" };
	private KeywordsFlow keywordsFlow;
	private Button btnSearch;
	private GestureDetector mGestureDetector;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_activity);
		btnSearch = (Button) findViewById(R.id.button);
		btnSearch.setOnClickListener(this);
		keywordsFlow = (KeywordsFlow) findViewById(R.id.frameLayout1);
		keywordsFlow.setDuration(800l);
		keywordsFlow.setOnItemClickListener(this);
		// 添加
		feedKeywordsFlow(keywordsFlow, keywords);
		keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
		mGestureDetector = new GestureDetector(this, new MoveGestureListener());
	}

	private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
		Random random = new Random();
		for (int i = 0; i < KeywordsFlow.MAX; i++) {
			int ran = random.nextInt(arr.length);
			String tmp = arr[ran];
			keywordsFlow.feedKeyword(tmp);
		}
	}

	@Override
	public void onClick(View v) {
		if (v instanceof TextView) {
			String keyword = ((TextView) v).getText().toString();
			// Intent intent = new Intent();
			// intent.setAction(Intent.ACTION_VIEW);
			// intent.addCategory(Intent.CATEGORY_DEFAULT);
			// intent.setData(Uri.parse("http://www.google.com.hk/#q=" +
			// keyword));
			// startActivity(intent);
			final AnimationSet  mAnimationScaleAnimation = (AnimationSet) AnimationUtils.loadAnimation(
					this, R.anim.scaleset);
			 v.startAnimation(mAnimationScaleAnimation);
			Toast.makeText(SearchActivity.this, keyword, Toast.LENGTH_SHORT)
					.show();
			Log.e("Search", keyword);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mGestureDetector.onTouchEvent(event)) {
			return true;
		} else {
			return false;
		}
	}

	class MoveGestureListener extends GestureDetector.SimpleOnGestureListener {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (e1.getX() - e2.getX() > 20) {
				keywordsFlow.rubKeywords();
				// keywordsFlow.rubAllViews();
				feedKeywordsFlow(keywordsFlow, keywords);
				keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
				Toast.makeText(SearchActivity.this, "right",
						Toast.LENGTH_SHORT).show();
			}
			if (e2.getX() - e1.getX() > 20) {
				keywordsFlow.rubKeywords();
				// keywordsFlow.rubAllViews();
				feedKeywordsFlow(keywordsFlow, keywords);
				keywordsFlow.go2Show(KeywordsFlow.ANIMATION_OUT);
				Toast.makeText(SearchActivity.this, "left",
						Toast.LENGTH_SHORT).show();
			}
			return true;
		}
	}

}
